package scala.eclipse.pde.ui.templates.osgi;

import java.net.URL;
import java.util.ArrayList;
import java.util.Arrays;
import java.util.ResourceBundle;

import org.eclipse.core.resources.IProjectDescription;
import org.eclipse.core.runtime.CoreException;
import org.eclipse.core.runtime.IProgressMonitor;
import org.eclipse.core.runtime.Platform;
import org.eclipse.jface.wizard.Wizard;
import org.eclipse.jface.wizard.WizardPage;
import org.eclipse.pde.ui.templates.OptionTemplateSection;

import scala.eclipse.pde.ui.templates.AbstractScalaTemplateSection;
import scala.eclipse.pde.ui.templates.ScalaPDEPlugin;
/**
 * Template class to create OSGi scala plugins
 * @author Josh
 *
 */
public class HelloScalaOSGiTemplate extends AbstractScalaTemplateSection {
	public static final String SCALA_NATURE = "ch.epfl.lamp.sdt.core.scalanature";
	public static final String KEY_START_MESSAGE = "startMessage"; //$NON-NLS-1$
	public static final String KEY_STOP_MESSAGE = "stopMessage"; //$NON-NLS-1$
	public static final String KEY_APPLICATION_CLASS = "applicationClass"; //$NON-NLS-1$

	

	public HelloScalaOSGiTemplate() {
		setPageCount(1);
		addOption(KEY_START_MESSAGE, "Start Message:", "Hello World!", 0);
		addOption(KEY_STOP_MESSAGE, "Stop Message:", "Goodbye World!", 0);
	}
	
	public void addPages(Wizard wizard) {
		WizardPage page = createPage(0, "org.eclipse.pde.doc.user.rcp-mail");
		page.setTitle("Basic OSGi Scala Bundle");
		page.setDescription("This template creates a basic Scala OSGi bundle that prints text to the console when it starts and stops.");
		wizard.addPage(page);
		markPagesAdded();
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.ui.templates.BaseOptionTemplateSection#isDependentOnParentWizard()
	 */
	public boolean isDependentOnParentWizard() {
		return true;
	}

	/* (non-Javadoc)
	 * @see org.eclipse.pde.ui.templates.AbstractTemplateSection#getNumberOfWorkUnits()
	 */
	public int getNumberOfWorkUnits() {
		return super.getNumberOfWorkUnits() + 1;
	}
	
	@Override
	public String getSectionId() {
		return "helloScalaOSGi";
	}

	@Override
	protected void updateModel(IProgressMonitor monitor) throws CoreException {
		//First make sure the project has a scala nature.
		if(project != null && !project.hasNature(SCALA_NATURE)) {
			IProjectDescription description = project.getDescription();
			String[] natureIds = description.getNatureIds();
			ArrayList<String> newNatureIds = new ArrayList<String>(natureIds.length + 1);
			newNatureIds.add(SCALA_NATURE);
			newNatureIds.addAll(Arrays.asList(natureIds));
			description.setNatureIds(newNatureIds.toArray(new String[newNatureIds.size()]));
			project.setDescription(description, monitor);
		}
	}

	@Override
	protected URL getInstallURL() {
		return ScalaPDEPlugin.getDefault().getBundle().getEntry("/");
	}

	@Override
	protected ResourceBundle getPluginResourceBundle() {
		return Platform.getResourceBundle(ScalaPDEPlugin.getDefault().getBundle());
	}

	@Override
	public String[] getNewFiles() {
		return new String[0];
	}

	@Override
	public String getUsedExtensionPoint() {
		return null;
	}

}
